﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrivilegeObject
    {
        public static Privilege[] GetPrivileges()
        {
            return Query(null);
        }

        public static Privilege GetPrivilege(int id)
        {
            Privilege privilege = new Privilege();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrivilegeDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrivilegeDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable privileges = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            privilege.Id             = Convert.ToInt32(privileges.Rows[0][PrivilegeDesc.Id]);
            privilege.Name           = Convert.ToString(privileges.Rows[0][PrivilegeDesc.Name]);
            privilege.Attribute      = Convert.ToString(privileges.Rows[0][PrivilegeDesc.Attribute]);

            return privilege;
        }

        public static Privilege GetPrivilege(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrivilegeDesc.Name, ConditionExpress.EQUALS, name);

            Privilege[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Privilege[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrivilegeDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                Privilege[] privileges = new Privilege[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Privilege privilege = new Privilege();

                    privilege.Id               = Convert.ToInt32(table.Rows[i][PrivilegeDesc.Id]);
                    privilege.Name             = Convert.ToString(table.Rows[i][PrivilegeDesc.Name]);
                    privilege.Attribute        = Convert.ToString(table.Rows[i][PrivilegeDesc.Attribute]);

                    privileges.SetValue(privilege, i);
                }

                return privileges;
            }
            return null;
        }

        public static Int32 Add(Privilege privilege)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrivilegeDesc.Name,            privilege.Name);
            builder.InsertAdd(PrivilegeDesc.Attribute,       privilege.Attribute);
            builder.QueryTableName = PrivilegeDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrivilegeDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrivilegeDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Privilege privilege)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrivilegeDesc.Name,              privilege.Name);
            builder.UpdateAdd(PrivilegeDesc.Attribute,         privilege.Attribute);
            builder.ConditionAdd(PrivilegeDesc.Id, ConditionExpress.EQUALS, privilege.Id);
            builder.QueryTableName = PrivilegeDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
